package com.cll.pmis.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cll.pmis.common.Constants;
import com.cll.pmis.common.PageInfo;
import com.cll.pmis.common.Result;
import com.cll.pmis.common.annotation.HasPerms;
import com.cll.pmis.common.security.SysUser;
import com.cll.pmis.entity.Drug;
import com.cll.pmis.entity.Orders;
import com.cll.pmis.entity.Output;
import com.cll.pmis.service.IOrderService;
import com.cll.pmis.service.IOutputService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单管理
 * </p>
 *
 * @author chenliangliang
 * @since 2018-06-18
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private IOrderService orderService;
    private IOutputService outputService;

    protected OrderController(IOrderService orderService, IOutputService outputService) {
        this.orderService = orderService;
        this.outputService = outputService;
    }

    @HasPerms("order:select")
    @GetMapping("/list")
    public Result orderList(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,@RequestParam(value = "pageSize",required = false) Integer pageSize, SysUser sysUser){
        if (pageIndex==null||pageIndex<1){
            pageIndex=1;
        }
        if (pageSize==null||pageSize<1){
            pageSize=4;
        }
        PageInfo<Orders> pageInfo = orderService.list(sysUser.getId(), pageIndex, pageSize);
        return Result.ok(pageInfo);
    }


    @HasPerms("order:select")
    @GetMapping("/detail")
    public Result orderDetail(@RequestParam("orderId") String orderId){
        if (StringUtils.isBlank(orderId)){
            return Result.paramError();
        }
        Orders order = orderService.selectOne(new EntityWrapper<Orders>().eq("order_id", orderId));
        if (order==null){
            return Result.fail("该订单不存在");
        }

        List<Drug> orderDetail = orderService.getOrderDetail(order.getId());

        order.setList(orderDetail);

        return Result.ok(order);
    }

    @PostMapping("/confirm")
    public Result confirmPay(@RequestParam("orderId") String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return Result.paramError();
        }

        Orders order = orderService.selectOne(new EntityWrapper<Orders>().eq("order_id", orderId));

        if (order == null) {
            return Result.fail("订单不存在");
        }

        if (order.getStatus() != Constants.ORDER_STATUS_UNPAY) {
            return Result.fail("订单已支付");
        }

        //药品出库
        Orders od = new Orders();
        od.setId(order.getId());
        od.setUpdateTime(new Date());
        od.setStatus(Constants.ORDER_STATUS_PAY);
        if (orderService.updateById(od)) {
            Output output = new Output();
            output.setStatus(Constants.OUTPUT_STATUS_UNCHECK);
            output.setId(order.getId());
            outputService.updateById(output);
            //jmsTemplate.convertAndSend(Constants.OUTPUT_QUEUE, order.getId());
            return Result.ok("");
        }
        return Result.fail("确认支付失败，请稍后重试!");
    }

}

